package ahblue.cn.tanzi.ui.person;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import ahblue.cn.tanzi.R;
import ahblue.cn.tanzi.model.user.LoginResultModel;
import ahblue.cn.tanzi.model.user.LookHisToryModel;
import ahblue.cn.tanzi.ui.base.BaseActivity;
import ahblue.cn.tanzi.util.ImageLoadUtil;
import ahblue.cn.tanzi.view.FloatingItemDecoration;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author pink-jackstraw
 * @date 2018/11/10
 * @describe 看过的历史记录  https://github.com/binbinqq86/FloatingBarItemDecoration
 */
public class LookedHistoryActivity extends BaseActivity {

    @BindView(R.id.recy)
    RecyclerView recyview;
    @BindView(R.id.refresh)
    SwipeRefreshLayout swipeRefreshLayout;
    private MyAdapter adapter;
    private int page = 0;

    private Map<Integer,List<LookHisToryModel.DataModel>> datas = new HashMap<>();//模拟服务器返回数据
    private List<LookHisToryModel.DataModel> list=new ArrayList<>();//adapter数据源
    private Map<Integer,String> keys=new HashMap<>();//存放所有key的位置和内容


    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    swipeRefreshLayout.setRefreshing(false);
                    adapter.notifyDataSetChanged();
                    //swipeRefreshLayout.setEnabled(false);
                    break;
                default:
                    break;
            }
        }
    };

    public static void into(Activity activity){
        activity.startActivity(new Intent(activity,LookedHistoryActivity.class));
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_looked);
        ButterKnife.bind(this);
        initData();
    }

    private void initData() {

        LookHisToryModel.getLooked(this, LoginResultModel.getToken(this), new LookHisToryModel.VertifyListener() {
            @Override
            public void conver(List<LookHisToryModel> mlist) {
                String da="";
                if (null!=mlist){
                    if (mlist.size()>0){
                        // 准备头部数据源
                        for (int x=0;x<mlist.size();x++){
                            datas.put(x,mlist.get(x).data);
                        }
                        for (int i = 0; i < datas.size(); i++) {
//                            if (!da.equals(mlist.get(i).day)){
//                                keys.put(list.size(),mlist.get(i).day);
//                                da=mlist.get(i).day;
//                            }
                            keys.put(list.size(),mlist.get(i).day);
                            for (int j = 0; j < datas.get(i).size(); j++) {
                                list.add(datas.get(i).get(j));
                            }
                        }
                    }
                }
                initView();
                adapter.notifyDataSetChanged();
            }
        });

    }

    private void initView() {
        recyview.setLayoutManager(new LinearLayoutManager(this));
        //设置adapter
        adapter=new MyAdapter(list,this);
        final FloatingItemDecoration floatingItemDecoration=new FloatingItemDecoration(this, Color.BLUE,1,1);
        floatingItemDecoration.setKeys(keys);
        floatingItemDecoration.setmTitleHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,50,getResources().getDisplayMetrics()));
        recyview.addItemDecoration(floatingItemDecoration);

        //设置布局管理器
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        layoutManager.setOrientation(1);
        recyview.setLayoutManager(layoutManager);
        recyview.setHasFixedSize(true);
        recyview.setAdapter(adapter);

        swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
        swipeRefreshLayout.setSize(SwipeRefreshLayout.LARGE);
//        swipeRefreshLayout.setProgressBackgroundColor(R.color.colorAccent);
        //swipeRefreshLayout.setPadding(20, 20, 20, 20);
        //swipeRefreshLayout.setProgressViewOffset(true, 100, 200);
        //swipeRefreshLayout.setDistanceToTriggerSync(50);
        swipeRefreshLayout.setProgressViewEndTarget(true, 100);
        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        dataList.clear();
//                        page = 0;
//                        initData();
                        mHandler.sendEmptyMessage(1);
                    }
                }).start();
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {

        private List<LookHisToryModel.DataModel> datas;
        private Context mContext;
        public MyAdapter(List<LookHisToryModel.DataModel> datas,Context mContext){
            this.datas=datas;
            this.mContext=mContext;
        }
        @Override
        public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.activity_look_item, parent,
                    false));
        }

        @Override
        public void onBindViewHolder(MyViewHolder holder, int position) {
            holder.name.setText(list.get(holder.getAdapterPosition()).object_name);
            holder.sign.setText(list.get(holder.getAdapterPosition()).title);
            holder.time.setText(list.get(holder.getAdapterPosition()).hour);
            ImageLoadUtil.load(LookedHistoryActivity.this,list.get(holder.getAdapterPosition()).image,holder.iv);
        }

        @Override
        public int getItemCount() {
            return list.size();
        }

        class MyViewHolder extends RecyclerView.ViewHolder {
            TextView name,sign,time;
            ImageView iv,icon;

            public MyViewHolder(View view) {
                super(view);
                name = (TextView) view.findViewById(R.id.name);
                sign = (TextView) view.findViewById(R.id.sign);
                time = (TextView) view.findViewById(R.id.time);
                iv = (ImageView) view.findViewById(R.id.iv);
                icon = (ImageView) view.findViewById(R.id.icon);
            }
        }
    }

    @OnClick(R.id.btn_back)
    public void onViewClicked() {
        finish();
    }
}
